<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>popot-2.01: example-003.cc</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">popot-2.01
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">example-003.cc</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="comment">// Example of how to make use of the ABC algorithm on a custom evaluate function</span>


<span class="comment">// We first define the generator of random numbers</span>
<span class="preprocessor">#include &quot;<a class="code" href="rng__generators_8h.html">rng_generators.h</a>&quot;</span>
<span class="keyword">typedef</span> popot::rng::CRNG RNG_GENERATOR;

<span class="preprocessor">#include &quot;<a class="code" href="popot_8h.html">popot.h</a>&quot;</span>

<span class="keywordtype">double</span> evaluate(<span class="keywordtype">double</span> * x, <span class="keywordtype">size_t</span> dimension, <span class="keywordtype">size_t</span> &amp; count)
{
  <span class="keywordtype">double</span> * params = (<span class="keywordtype">double</span>*) x;
  <span class="keywordtype">double</span> fit = 0.0;
  <span class="keywordtype">double</span> y_i, y_i_1;
  <span class="keywordflow">for</span>(<span class="keywordtype">size_t</span> i = 0 ; i &lt; dimension-1 ; ++i)
    {
      y_i = params[i];
      y_i_1 = params[i+1];
      fit += 100 * pow(y_i_1 - pow(y_i,2.0),2.0)+pow(y_i - 1.0,2.0);
    }
  count ++;

  <span class="keywordflow">return</span> fit;
}

<span class="keywordtype">bool</span> stop(<span class="keywordtype">double</span> f, <span class="keywordtype">int</span> epoch)
{
  <span class="keywordflow">return</span> (f &lt;= 1e-2) || (epoch &gt;= 10000);
}

<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> * argv[])
{
  RNG_GENERATOR::rng_srand();

  <span class="keywordtype">size_t</span> colony_size = 50;
  <span class="keywordtype">size_t</span> dimension = 15;
  <span class="keywordtype">size_t</span> count=0;
  
  <span class="keyword">auto</span> algo = <a name="a0"></a><a class="code" href="namespacepopot_1_1algorithm.html#ac3dab4425f17e55c3f7b8305d31a2051">popot::algorithm::abc</a>(colony_size, dimension,
                                    [] (<span class="keywordtype">size_t</span> index) -&gt; <span class="keywordtype">double</span> { <span class="keywordflow">return</span> -10; },
                                    [] (<span class="keywordtype">size_t</span> index) -&gt; <span class="keywordtype">double</span> { <span class="keywordflow">return</span>  10; },
                                    stop,
                                    [&amp;count,dimension] (<span class="keywordtype">double</span> * params) -&gt; <span class="keywordtype">double</span> { <span class="keywordflow">return</span> evaluate(params, dimension, count);});

  algo.init();
  algo.run();

  std::cout &lt;&lt; <span class="stringliteral">&quot;Best minimum found :&quot;</span> &lt;&lt; algo.getBest().getFValue() &lt;&lt; <span class="stringliteral">&quot; in &quot;</span> &lt;&lt; algo.getEpoch() &lt;&lt; <span class="stringliteral">&quot; steps &quot;</span> &lt;&lt; std::endl;
  std::cout &lt;&lt; <span class="stringliteral">&quot;Position of the optimum : &quot;</span> &lt;&lt; algo.getBest() &lt;&lt; std::endl;
  std::cout &lt;&lt; std::endl;
  std::cout &lt;&lt; (int)count &lt;&lt; <span class="stringliteral">&quot; Function Evaluations&quot;</span> &lt;&lt; std::endl;
  
}
</pre></div> </div>
</div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.5
</small></address>

</body>
</html>
